/*
 * @Author: chunlai
 * @Date: 2025-07-16 16:38:40
 * @LastEditTime: 2025-09-05 08:53:54
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \02.bldinsure\src\views\sign\hebao.js
 * @Description:
 */
import {
  payment,
  paymentAuthApply,
  paymentAuthConfirm,
} from "@/utils/functions.js";
import { rebackUrlPath } from "@/methods/format.js";
export const billHandleMixin = {
  methods: {
    /**网销产品核保成功操作*/
    netBillSuccess() {
      if (this.compEngName == "huaan") {
        return this.underwriteApi();
      }
      if (this.compEngName == "ansheng" && this.renewalInfo.renewFlag == "-1") {
        return this.asBankSign();
      }
      this.netPayApi();
    },
    asBankSign() {
      this.$store.commit("updateLoad", true);
      let params = {
        ...this.apiComParams,
        signType: "01",
      };
      paymentAuthApply(params)
        .then(({ data }) => {
          if (data.code == "1") {
            this.smspop = true;
            this.$nextTick(() => {
              this.$refs.smsCodeRef.initModule(this.renewalInfo.firBankMobile);
            });
          } else {
            this.$store.commit("json/SET_AS_BILL_STATUS", "1");
            this.$msAlert(`${data.msg || "签约失败"}，可更换银行卡重新签约`);
          }
        })
        .finally(() => {
          this.$store.commit("updateLoad", false);
        });
    },
    payAuthSureApi(type = "", smsCode = "") {
      this.smspop = false;
      this.$store.commit("updateLoad", true);
      let params = {
        ...this.apiComParams,
        signType: "01",
        smsCode: smsCode,
      };
      paymentAuthConfirm(params)
        .then(({ data }) => {
          if (data.code == "1") {
            this.netPayApi();
          } else {
            this.$store.commit("json/SET_AS_BILL_STATUS", "1");
            this.$msAlert(`${data.msg || "签约失败"}，可更换银行卡重新签约`);
          }
        })
        .finally(() => {
          this.$store.commit("updateLoad", false);
        });
    },
    // 网销
    netPayApi() {
      let params = {
        ...this.apiComParams,
      };
      if (this.isNetInsure == "1") {
        params.successUrl = `${rebackUrlPath("#/billres")}`;
        params.failUrl = `${rebackUrlPath("#/billres")}`;
      } else if (this.compEngName == "ansheng") {
        let payType = "7";
        if (this.origin === "pc") {
          payType = "1";
        }
        params.payType = payType;
        params.payAmount = this.totalPrem;
        this.$store.commit("json/SET_AS_BILL_STATUS", "3");
      }
      this.$store.commit("updateLoad", true);
      payment(params)
        .then(({ data }) => {
          // 跳转第三方平台支付
          if (data.code == "5" && data.payUrl) {
            if (this.compEngName == "ansheng") {
              this.sendForm(data.data, data.payUrl);
            } else {
              window.location.href = data.payUrl;
            }
          } else {
            this.$msAlert(data.msg);
          }
        })
        .finally(() => {
          this.$store.commit("updateLoad", false);
        });
    },
    sendForm(data, url) {
      this.$store.commit("updateLoad", true);
      this.$nextTick(() => {
        var form = document.createElement("form");
        form.id = "my-form";
        document.body.appendChild(form);
        let input = document.createElement("input");
        input.type = "hidden";
        input.name = "requestDoc";
        input.value = data;
        form.setAttribute("accept-charset", "gbk");
        form.appendChild(input);
        form.method = "GET";
        form.action = url;
        form.submit();
      });
      document.body.removeChild(form);
      this.$store.commit("updateLoad", false);
    },
  },
};
